package com.codedef.util;

/**
 * @author 孙宏宇
 * @date 2021/3/4 19:35
 */
public class UriParseUtil {


    public static PathNode createPathTree(String[] checkPath) {
        PathNode root = new PathNode();
        // 遍历每个要拦截的路径
        for (String uri : checkPath) {
            PathNode p = root;
            // 遍历一个URI中的每一小段
            for (String single : uri.substring(1).split("/")) {
                p.addChild(single);
                p = p.get(single);
            }

        }
        return root;
    }

    public static boolean needToIntercept(PathNode root, String uri) {
        PathNode p = root;
        // 遍历一个URI中的每一小段
        for (String single : uri.substring(1).split("/")) {
            if (p.containsKey("*"))
                return true;
            else if (!p.containsKey(single))
                return false;
            p = p.get(single);
        }
        return true;
    }

}
